package com.example.bookstore.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.bookstore.entity.Book;
import com.example.bookstore.entity.User;
import com.example.bookstore.mapper.UserMapper;
import com.example.bookstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void Insert(User user) {
            userMapper.insert(user);
    }

    @Override
    public User Select(int id) {
        User result = userMapper.select(id);
        return result;
    }

    @Override
    public void Delete(int id) {
        userMapper.delete(id);
    }

    @Override
    public void Update(User user, int id) {
        userMapper.update(user,id);
    }

    @Override
    public String Selects(String thing, int id) {
        return userMapper.selects(thing,id);
    }

    @Override
    public ArrayList<User> selectAll() {
        return  userMapper.selectAll();
    }


    public void insert(User user){
        userMapper.insert(user);
    }

    public String userExsit(User user){

        List<User> initUsers = new ArrayList<User>();
        User user1 = new User("test1", "test1");
        User user2 = new User("test2", "test2");

        initUsers.add(user1);
        initUsers.add(user2);

        List<String> userList = new ArrayList<String>();
        for (User loginUser: initUsers) {
            System.out.println("loginUser.username====" + loginUser.username);

            userList.add(loginUser.username);

            if (user.username.equals(loginUser.username) && !user.password.equals(loginUser.password)) {
                return "用户名/密码错误";
            } else if (user.username.equals(loginUser.username) && user.password.equals(loginUser.password)){
                return "成功";
            }
        }

        if (!userList.contains(user.username)) {
            return "用户不存在";
        }
            return null;
    }




}
